using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

using MicroFractal.iTunesBrowser.Business;
using MicroFractal.iTunesBrowser.Core.Data;
using MicroFractal.iTunesBrowser.Core.Device;

namespace MicroFractal.iTunesBrowser.Core
{
    public sealed class LibraryManager
    {

        #region Attributes

        private static Dictionary<Device.IDevice, Library> _libraries = new Dictionary<MicroFractal.iTunesBrowser.Core.Device.IDevice, Library>();

        private const string K_MediaFolder = ":private:var:root:Media";

        #endregion

        #region Public Properties

        static List<string> _imageExtensions;
        public static List<string> ImageExtensions
        {
            get 
            {
                if (_imageExtensions == null)
                {
                    Stream s = File.Open(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Properties.Resources.ImageExtensions),FileMode.Open);
                    _imageExtensions  = XmlSerializer.FromTypes(new Type[]{typeof(List<string>)})[0].Deserialize(s) as List<string>;
                }

                return _imageExtensions; 
            }
            set { _imageExtensions = value; }
        }

        static List<string> _musicExtensions;
        public static List<string> MusicExtensions
        {
            get 
            {
                if (_musicExtensions == null)
                {
                    Stream s = File.Open(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Properties.Resources.MusicExtensions), FileMode.Open);
                    _musicExtensions = XmlSerializer.FromTypes(new Type[]{ typeof(List<string>)})[0].Deserialize(s) as List<string>;
                }    
                return _musicExtensions; 
            }
            set { _musicExtensions = value;}
        }

        #endregion

        #region Public Methods

        public static Library GetLibrary()
        {
            if (DeviceManager.Instance.Devices.Count == 0)
                throw new Device.DevicesNotFoundException();

            return GetLibrary(DeviceManager.Instance.Devices[0], false);
        }

        public static Library GetLibrary(Device.IDevice device, bool refresh)
        {
            if (device == null)
                throw new ArgumentNullException("device");

            if (!_libraries.ContainsKey(device))
            {
                _libraries.Add(device, DbParser.GetDbParser(device, device.GetDBFile()).GetLibrary());
            }
            else
            {
                if (refresh)
                    _libraries[device] = DbParser.GetDbParser(device, device.GetDBFile(refresh)).GetLibrary();
            }

            return _libraries[device];
        }

        #endregion

        #region internal Methods

        internal static void RemoveLibrary(IDevice device)
        {
            _libraries.Remove(device);
        }

        #endregion

        #region Private Methods

        private static void CheckDevice(Device.BaseDevice device)
        {
            if (device == null)
                throw new ArgumentNullException("device");

            if (!device.IsConnected)
                throw new Exception(); //TODO Ver el tipo de exception
        }

        #endregion

    }

}
